<?php
class controller{
	protected $model = NULL; //数据库模型
	protected $layout = NULL; //布局视图
	private $_data = array();
	
	protected function init(){}
	
	public function __construct(){
		if( 1 != config('APP_STATE') ){
			$this->alert('该应用尚未开启!');
		}
		$this->model = model('base')->model;
		$this->init();
	}

	public function __get($name){
		return isset( $this->_data[$name] ) ? $this->_data[$name] : NULL;
	}

	public function __set($name, $value){
		$this->_data[$name] = $value;
	}
	
	//获取模板对象
	protected function view(){
		static $view = NULL;
		if( empty($view) ){
			$view = new cpTemplate( config('TPL') );
		}
		return $view;
	}
	
	//模板赋值
	protected function assign($name, $value){
		return $this->view()->assign($name, $value);
	}
	
	//模板显示
	protected function display($tpl = '', $return = false, $is_tpl = true ){
		if( $is_tpl ){
			$tpl = empty($tpl) ? CONTROLLER_NAME . '_'. ACTION_NAME : $tpl;
			if( $is_tpl && $this->layout ){
				$this->__template_file = $tpl;
				$tpl = $this->layout;
			}
		}
		$this->view()->config['TPL_TEMPLATE_PATH'] = BASE_PATH . 'apps/' . config('_APP_NAME') . '/view/';
		$this->view()->assign( $this->_data );
		return $this->view()->display($tpl, $return, $is_tpl);
	}
	
	//判断是否是数据提交	
	protected function isPost(){
		return $_SERVER['REQUEST_METHOD'] == 'POST';
	}
	
	//直接跳转
	protected function redirect( $url, $code=302) {
		header('location:' . $url, true, $code);
		exit;
	}
	
	//弹出信息
	protected function alert($msg, $url = NULL, $parent=false){
		header("Content-type: text/html; charset=utf-8"); 
		$alert_msg="alert('$msg');";
		if( empty($url) ) {
			$gourl = 'history.go(-1);';
		}else{
			$gourl = ($parent ? 'parent': 'window') . ".location.href = '{$url}'";
		}
		echo "<script>$alert_msg $gourl</script>";
		exit;
	}
	
	//获取分页查询limit
	protected function pageLimit($url, $num = 10){
		$url = str_replace(urlencode('{page}'), '{page}', $url);
		$page = is_object($this->pager['obj']) ? $this->pager['obj'] : new Page();	
		$cur_page = $page->getCurPage($url);
		$limit_start = ($cur_page-1) * $num;
		$limit = $limit_start.','.$num;
		$this->pager = array('obj'=>$page, 'url'=>$url, 'num'=>$num, 'cur_page'=>$cur_page, 'limit'=>$limit);
		return $limit;
	}
	
	//分页结果显示
	protected function pageShow($count){
		return $this->pager['obj'] ->show($this->pager['url'], $count, $this->pager['num']);
	}

    //ajax返回信息
    protected function jsonMsg($msg,$status = 0,$url =NULL){
        echo json_encode(array("info" => $msg, "status" => $status, "url" => $url));
        exit;
    }

    //页面信息提示
    public function pageMsg($msg,$state = null,$url=NULL) {       
        if (!headers_sent())header("Content-type: text/html; charset=utf-8");
        echo'<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"> 
<title>';if($state){echo '成功提示';}else{ echo '错误提示';}echo'</title>
<style>
body{font:14px/2.0 "Lucida Grande","微软雅黑";color:#333;background:#fff;padding:0px;margin:0px;}
a{text-decoration:none;color:#3071BF}
a:hover{text-decoration:none;}
.error{margin:30px auto;width:90%;}
.error .error_title{border-bottom:1px #eee solid;font-size:25px;line-height:60px; height:60px;font-weight:600;margin-bottom:10px;background}
.error .error_box{border-left:4px solid #ffb61e;font-size:14px;line-height:25px;padding:6px 15px;background:#FFE}
.error .error_tip{margin-top:10px;padding:6px;font-size:14px;padding-left:15px;background:#f0f0f4}
</style>
</head>
<body>
<div class="error">
<div class="error_title">';
if($state){
	echo '^_^ 成功提示';
}else{
    echo ':( 错误提示';
}
echo '</div>
<div class="error_box">'.$msg.'</div>';
if($url){
echo '<div class="error_tip">页面自动&nbsp;<a Id="href" href="'.$url.'" title="跳转">跳转</a>&nbsp; 等待时间： <b id="wait">3</b> [ <a href="'.$url.'">立即跳转</a> ]</div>
</div>';}else{
echo '<div class="error_tip">您可以选择 &nbsp;&nbsp;<a href="'.__ROOT__.'" title="重试">重试</a>&nbsp;&nbsp;<a href="javascript:history.back()" title="返回">返回</a>&nbsp;&nbsp;或者&nbsp;&nbsp;<a href="/" title="回到首页">回到首页</a> </div>';
};
if($url){
echo'
<script type="text/javascript">
(function(){
    var wait = document.getElementById(\'wait\'),href = document.getElementById(\'href\').href;
    var interval = setInterval(function(){
    var time = --wait.innerHTML;
    if(time == 0) {
        location.href = href;
        clearInterval(interval);
    };
}, 3000);
})();
</script>';
}
echo'
</body>
</html>';
exit;
    }
    //页面404错误提示
    public function pageError(){   
        header('HTTP/1.1 404 Not Found');
        header("status: 404 Not Found");
        $this->pageMsg('404 Not Found 页面不存在。');
    }  
}